// api/server.ts
import { ApolloServer, AuthenticationError } from "apollo-server";
import { schema } from "./schema";
import { db } from "./db";
import { getUserId } from "./utils";

export const server = new ApolloServer({
  schema,
  context: ({ req }) => {
    // console.log("req:", req);
    // Get the user token from the headers.
    const userId = getUserId(req);
    // userId is null ,maybe login request
    console.log("user id:", userId);
    if (userId === null) {
      console.log("user id is null");
      console.log("req:", req);
      //todo: check SigninMutation command
    } else {
      return {
        db,
        userId,
      };
    }
  },
});
